package com.sitech.portal.system.service.impl;

import com.sitech.ibnms.common.form.TreeNode;
import com.sitech.ibnms.common.service.IAsyncTreeService;
import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.portal.system.dao.ISysDeptDao;
import com.sitech.portal.system.dao.ISysRoleDao;
import com.sitech.portal.system.dao.ISysWorkGroupDao;
import com.sitech.portal.system.domain.TblSYS_WORKGROUP;
import com.sitech.portal.system.service.ISysWorkGroupService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SysWorkGroupServiceImpl implements ISysWorkGroupService,IAsyncTreeService {

	private ISysWorkGroupDao sysWorkGroupDao;
	private ISysDeptDao sysDeptDao;
    private ISysRoleDao sysRoleDao;

    public void setSysRoleDao(ISysRoleDao sysRoleDao) {
        this.sysRoleDao = sysRoleDao;
    }
	public ISysDeptDao getSysDeptDao() {
		return sysDeptDao;
	}

	public void setSysDeptDao(ISysDeptDao sysDeptDao) {
		this.sysDeptDao = sysDeptDao;
	}

	public ISysWorkGroupDao getSysWorkGroupDao() {
		return sysWorkGroupDao;
	}

	public void setSysWorkGroupDao(ISysWorkGroupDao sysWorkGroupDao) {
		this.sysWorkGroupDao = sysWorkGroupDao;
	}

	@Override
	public void deleteUserFromWorkGroup(String login_id, String workGroupId) {
		sysWorkGroupDao.deleteUserFromWorkGroup(login_id, workGroupId);
	}

	@Override
	public void deleteWorkGroup(String workGroupId) {
		sysWorkGroupDao.deleteWorkGroup(workGroupId);
		sysWorkGroupDao.deleteAllUserFromWorkGroup(workGroupId);
	}

	public List<HashMap> findAllWorkGroup(){
		return sysWorkGroupDao.findAllWorkGroup();
	}
	
	@Override
	public TblSYS_WORKGROUP findWorkGroupById(String id) {
		return sysWorkGroupDao.findWorkGroupById(id);
	}

	@Override
	public List<HashMap> findWorkGroupByParentId(String parentId) {
		return sysWorkGroupDao.findWorkGroupByParentId(parentId);
	}
	
	public List<HashMap> findWorkGroupByParentIdUser(String parentId,String userId){
		List list = sysDeptDao.findDeptByMgrId(userId);
		if(list!=null && list.size()>0)
			return sysWorkGroupDao.findWorkGroupByParentIdUser(parentId,userId);
		else
			return sysWorkGroupDao.findWorkGroupByParentId(parentId);
	}

	@Override
	public IPage finedUsersOfWorkGroup(String workGroup_id, PageForm pageForm) {
		return sysWorkGroupDao.finedUsersOfWorkGroup(workGroup_id, pageForm);
	}

	@Override
	public void insertUserToWorkGroup(String login_id, String workGroupId) {
		List list = sysWorkGroupDao.findUserOfWorkGroup(login_id,workGroupId);
		if(list!=null && list.size()==0)
			sysWorkGroupDao.insertUserToWorkGroup(login_id, workGroupId);
	}
	
	public void insertUserToWorkGroup(String login_id, String[] workGroupIds){
		sysWorkGroupDao.deleteWorkGroupByUser(login_id);
		if(workGroupIds!=null){
			for(String workGroupId : workGroupIds){
				sysWorkGroupDao.insertUserToWorkGroup(login_id, workGroupId);
			}
		}
	}

	@Override
	public void insertWorkGroup(TblSYS_WORKGROUP wp) {
		sysWorkGroupDao.insertWorkGroup(wp);
	}

	@Override
	public void updateWorkGroup(TblSYS_WORKGROUP wp) {
		sysWorkGroupDao.updateWorkGroup(wp);
	}

	@Override
	public void deleteAllUserFromWorkGroup(String workGroupId) {
		sysWorkGroupDao.deleteAllUserFromWorkGroup(workGroupId);
	}

	@Override
	public TreeNode getRootNode(String loginId, String rootId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TreeNode> getSubNodes(String loginId, TreeNode pnode)
			throws Exception {
		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		String id = pnode.getId();
		if("0".equals(id))
			id = null;
		List roles = null;
        Boolean isAdmin =   sysRoleDao.isAdmin(loginId);
        if(id==null&&!isAdmin)
			roles = this.findWorkGroupByParentIdUser(id,loginId);
		else
			roles = this.findWorkGroupByParentId(id);
		for(Object obj : roles){
			TreeNode treeNode = new TreeNode();
			HashMap map = (HashMap)obj;
			treeNode.setId(""+map.get("WORKGROUP_ID"));
			treeNode.setName(""+map.get("WORKGROUP_NAME"));
			treeNode.setpId(pnode.getId());
			if("0".equals(map.get("CHILDREN").toString()))
				treeNode.setIsParent(false);
			treeNodes.add(treeNode);
		}
		return treeNodes;
	}
	

}
